package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdMammonInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_ELIMINATION_MAMMON_HELP, desc = "财神助力")
public class MammonHelpCmd extends AbstractCommand {
    private int type;
    private List<Integer> idList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.MammonHelpReqMsg req = StrongholdWarProto.MammonHelpReqMsg.parseFrom(packet.getBytes());
        type = req.getType();
        idList = req.getIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        String unionUid = player.getUserInfo().getUnionUid();
        String mammonConsume = StrongholdActivityMgr.getMammonConsume(type);
        ret = checkCanUseMammonHelp(player, activityInfo, unionUid, mammonConsume);
        if (ret != 0) {
            sendToPlayer(player);
            return;
        }

        if (type == 1) {
            //指定商会
            if (idList.size() != 1) {
                ret = GameErrorCode.E_STRONGHOLD_MAMMON_HELP_NO_APPOINT_UNION;
                sendToPlayer(player);
                return;
            }
        }


        Property property = PropertyHelper.parseStringToProperty(mammonConsume);
        GoodsInfo goodsInfo = null;
        for (Integer goodsId : property.getGoods().keySet()) {
            goodsInfo = CommonMgr.getGoodsInfo(goodsId);
        }

        int effectType = StrongholdActivityMgr.getMammonHelpEffectType();
        //伤害效果
        UserPatrons patrons = PatronsMgr.getUserPatronsByEarnSpeedRank(1,player.getModule(PatronsModule.class).getUserPatronsList());
        StrongholdMammonInfo mammonInfo = calcDamage(type, goodsInfo.getExtendParam(), patrons.getAbility());
        StrongholdWarProto.CrossStrongholdMammonHelpReqMsg.Builder req = StrongholdWarProto.CrossStrongholdMammonHelpReqMsg.newBuilder();
        req.setActivityId(activityInfo.getActivityId());
        req.setType(effectType);
        req.addAllId(idList);
        req.setDamage(mammonInfo.getTotalDamage().toString());
        req.setAttackTimes(mammonInfo.getAttackTimes());
        req.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        req.setGoodsId(goodsInfo.getGoodsId());
        req.setMammonType(type);
        player.sendUnionActivityGroupPacket(Protocol.C_CROSS_STRONGHOLD_MAMMON_HELP, req, req.getActivityId());
    }

    private int  checkCanUseMammonHelp(GamePlayer player, ActivityInfo activityInfo, String unionUid, String mammonConsume) {
        if (activityInfo == null) {
            return GameErrorCode.E_STRONGHOLD_NOT_FOUND;
        }
        if (!StrongholdActivityMgr.isEliminateTime()) {
            return GameErrorCode.E_STRONGHOLD_ELIMINATE_NO_OPEN;
        }
        boolean hasQualification = StrongholdWarMgr.hasQualification(activityInfo.getActivityId(), unionUid);
        if (!hasQualification) {
            return GameErrorCode.E_STRONGHOLD_NO_QUALIFICATION;
        }
        if (mammonConsume == null) {
            return GameErrorCode.E_BAG_ITEM_NO_EXIST;
        }
        Property property = PropertyHelper.parseStringToProperty(mammonConsume);
        GoodsInfo goodsInfo = null;
        for (Integer goodsId : property.getGoods().keySet()) {
            goodsInfo = CommonMgr.getGoodsInfo(goodsId);
        }
        if (goodsInfo == null) {
            return GameErrorCode.E_BAG_ITEM_NO_EXIST;
        }
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        return 0;
    }

    private StrongholdMammonInfo calcDamage(int type, String paramStr, BigInteger patronsAbility) {
        String s = paramStr.split("-")[type - 1];
        String[] arr = s.split("\\|");
        if (type == 1) {
            BigInteger totalDamage = new BigDecimal(patronsAbility).add(new BigDecimal(arr[3]))
                    .multiply(new BigDecimal(arr[0])).multiply(new BigDecimal(arr[1])).divide(new BigDecimal("1000"),2,BigDecimal.ROUND_HALF_UP).toBigInteger();
            return new StrongholdMammonInfo(totalDamage, 1);
        }else {
            BigInteger totalDamage = new BigDecimal(patronsAbility).add(new BigDecimal(arr[3])).multiply(new BigDecimal(arr[1])).divide(new BigDecimal("1000"),2,BigDecimal.ROUND_HALF_UP).toBigInteger();
            int attckTimes = Integer.parseInt(arr[0]);
            return new StrongholdMammonInfo(totalDamage, attckTimes);
        }
    }

    private void sendToPlayer(GamePlayer player) {
        StrongholdWarProto.MammonHelpRespMsg.Builder resp = StrongholdWarProto.MammonHelpRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_MAMMON_HELP, resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
